﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using Maticsoft.Common;
using DQYT.HTDZ.DBUtility;
using DQYT.HTDZ.BusinessService;
using System.Xml;
using System.Text;
using System.Drawing;
using DQYT.HTDZ.Common;
using DQYT.HTDZ.BLL;
using DQYT.HTDZ.Model;
namespace DQYT.HTDZ.Web.Views.SJCJDB
{
    /// <summary>
    /// 临时access数据库采集类
    /// </summary>
    public partial class TestAccessIn : System.Web.UI.Page
    {
        private IConfigurature dbConf;
        private List<CJSJModel> cjsjModels = new List<CJSJModel>();//存放采集数据集合
        private BDDATESOURCEModel BbDatasourceModel = new BDDATESOURCEModel();
        private CjsjParser cjsjParser;//工具类，转换采集数据
        private string FileName = ""; //上传的文件全名
        private DataTable JHTable;
        private DataTable TimeTb;//存放检索出的开始时间和结束时间
        
        protected void Page_Load(object sender, EventArgs e)
        {
              fulFileUpload.Enabled = true;
              hfdUploadFile.Visible = true;
              this.cjsjParser = new CjsjParser();
        }

        //protected void btnUploadData_Click(object sender, EventArgs e)
        protected void btnUploadData_Click(object sender, EventArgs e)
        {
           
            if (fulFileUpload.HasFile)
            {

                //保存本地数据文件全名到隐藏控件中
                string serverPath = Server.MapPath("../../SJCJDataFile/DBFile/") + fulFileUpload.FileName.Trim().ToString().ToLower();
                fulFileUpload.PostedFile.SaveAs(serverPath);
                hfdUploadFile.Value = serverPath;
                this.FileName = fulFileUpload.FileName.Trim().ToString().ToLower();
                string[] info = this.GetFileInfo();
                dbConf = new AccessConfigurature(info, " ");//初始化本地数据库配之类
                bool confirm = AccessIn();
                if (confirm == true)
                {
                    
                    //返回父页面一个值Y
                    string strScript = "<script language=javascript>top.window.returnValue=";
                    strScript += "\"Y\"" + ";top.window.close(this);</script>";
                    Response.Write(strScript);
                }
                else
                {
                    JScript.Alert(this.Page, "该数据已存在，请选择其它文件！");
                }

            }
        }

        /// <summary>
        /// 获取数据库文件名字串数组
        /// </summary>
        /// <returns></returns>
        private string[] GetFileInfo()
        {
            #region
            string[] info = new string[2];
            info[0] = hfdUploadFile.Value.ToString();
            info[1] = "T_SJCJB_QX_TEMP";
            return info;
            #endregion
        }
        /// <summary>
        /// 文件上传内容框内容改变时触发该事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void hfdUploadFile_ValueChanged(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 导入数据方法
        /// </summary>
        /// <returns></returns>
        protected bool AccessIn()
        {
            //获取"T_SJCJB_QX_TEMP"表中的所有井号,放在一个table中
            //循环table的count次，
            //先取出井号和时间
            //每次循环255次，每次都取出当前井号的一条记录，比如，电流，电压，
            string sqlstr = "select distinct JH from T_SJCJB_QX_TEMP GROUP BY JH";
           this.JHTable = this.dbConf.GetQueryResultSet(sqlstr);//保存非重复井号
           DataTable curetb =this.JHTable.Copy();
           curetb.Clear();
           for (int i = 0; i < this.JHTable.Rows.Count; i++)
            {
                //取出井号和时间保存到采集表中
                this.BbDatasourceModel.WELLID = JHTable.Rows[i][0].ToString();
                //string date = this.FileName.Substring(0, this.FileName.Length - 4);
                string date = this.FileName.Substring(0, 14);
                this.BbDatasourceModel.JCRQ = TimeParser.strToDateTime(date);
                this.BbDatasourceModel = transBDDATESOURCEModel(this.BbDatasourceModel);
                //判断要采集数据是否已经存在
                DateTime jcrq = this.BbDatasourceModel.JCRQ.HasValue ? this.BbDatasourceModel.JCRQ.Value : DateTime.Now;
                StandardService service = new StandardService();
                if (service.Exists_idJCRQ(BbDatasourceModel.WELLID, jcrq))
                {
                    //JScript.Alert(this.Page, "该数据已存在，请选择其它文件！");
                    //continue;
                    curetb.Rows.Add(this.JHTable.Rows[i].ItemArray);
                }
           }
           if(curetb.Rows.Count < 1)
           {
               return false;
           }
           else
           {
               string sqlStr1 = "";
            //按不重复井号个数循环
               for (int i = 0; i < curetb.Rows.Count; i++)
            {
                //取出井号和时间保存到采集表中
                this.BbDatasourceModel.WELLID = curetb.Rows[i][0].ToString();
                string date = this.FileName.Substring(0, 14);
                this.BbDatasourceModel.JCRQ = TimeParser.strToDateTime(date);
                //提取开始时间和结束时间
                //this.TimeTb.Clear();
                sqlStr1 = "select TIME_S,TIME_E from T_SJCJB_TEMP  where JH='" + this.BbDatasourceModel.WELLID + "'";
                this.TimeTb = this.dbConf.GetQueryResultSet(sqlStr1);
                this.BbDatasourceModel.KSSJ = (DateTime)this.TimeTb.Rows[0][0];
                this.BbDatasourceModel.JSSJ = (DateTime)this.TimeTb.Rows[0][1];
                this.BbDatasourceModel = transBDDATESOURCEModel(this.BbDatasourceModel);
                //提取CJSJ到CJSJModel中
                for (int j = 0; j < 256; j++)
                {
                    CJSJModel sjcjModel = new CJSJModel();//每条记录保存一次
                    string XH = "";//序号，
                    if (j < 10)
                        XH = "00" + j.ToString();
                    else if (j < 100)
                        XH = "0" + j.ToString();
                    else
                        XH = j.ToString();
                    //string sqlStr = "select XH,SJ1 from T_SJCJB_QX_TEMP where XH like '*" + XH + "' and JH='" + JHTable.Rows[i][0].ToString() + "'";
                    string sqlStr = "select XH,SJ1 from T_SJCJB_QX_TEMP where XH like '_" + XH + "' and  JH='" + curetb.Rows[i][0].ToString() + "'";
                    DataTable CJSJTabel = this.dbConf.GetQueryResultSet(sqlStr);//保存单条采集数据
                    //将数据放入sjcjModel中
                    sjcjModel.HDL = (float)Convert.ToDouble(CJSJTabel.Rows[2][1].ToString());//合电流
                    sjcjModel.HYGGL = (float)Convert.ToDouble(CJSJTabel.Rows[0][1].ToString());//合有功功率
                    sjcjModel.DJSCNJ = (float)Convert.ToDouble(CJSJTabel.Rows[1][1].ToString());//电机输出扭矩
                    sjcjModel.HGLYS = (float)Convert.ToDouble(CJSJTabel.Rows[3][1].ToString());//合功率因数
                    sjcjModel.HWGGL = (float)Convert.ToDouble(CJSJTabel.Rows[4][1].ToString());//合无功功率
                    sjcjModel.HSZGL = (float)Convert.ToDouble(CJSJTabel.Rows[5][1].ToString());//合视在功率
                    sjcjModel.HDY = (float)Convert.ToDouble(CJSJTabel.Rows[6][1].ToString());//合电压
                    sjcjModel = transSjcjModel(sjcjModel);
                    this.cjsjModels.Add(sjcjModel);//将单条记录添加到集合中
                }
                this.BbDatasourceModel.CJSJ = BinarySerializer.SerializeObject(this.cjsjModels);//将采集集合转换成二进制集合
                BDDATESOURSEBLL BdDataSourseBll = new BDDATESOURSEBLL();
                BDValueFunc valueFunc = new BDValueFunc();
                valueFunc.setValue(this.BbDatasourceModel);
                BdDataSourseBll.Add(this.BbDatasourceModel);
                }
            }

            return true;
        }

        /// <summary>
        /// 将采集动态数据经过转换算法转换后形成真正能入库的数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public CJSJModel transSjcjModel(CJSJModel model)
        {

            model.AYGGL = (model.AYGGL == null) ? -1 : model.AYGGL.Value;
            model.BYGGL = (model.BYGGL == null) ? -1 : model.BYGGL.Value;
            model.CYGGL = (model.CYGGL == null) ? -1 : model.CYGGL.Value;
            model.PL = (model.PL == null) ? -1 : model.PL.Value;
            model.HYGGL = (model.HYGGL == null) ? -1 : model.HYGGL.Value;
            model.AWGGL = (model.AWGGL == null) ? -1 : model.AWGGL.Value;
            model.BWGGL = (model.BWGGL == null) ? -1 : model.BWGGL.Value;
            model.CWGGL = (model.CWGGL == null) ? -1 : model.CWGGL.Value;
            model.HWGGL = (model.HWGGL == null) ? -1 : model.HWGGL.Value;
            model.ADY = (model.ADY == null) ? -1 : model.ADY.Value;
            model.BDY = (model.BDY == null) ? -1 : model.BDY.Value;
            model.CDY = (model.CDY == null) ? -1 : model.CDY.Value;
            model.HDY = (model.HDY == null) ? -1 : model.HDY.Value;
            model.ADL = (model.ADL == null) ? -1 : model.ADL.Value;
            model.BDL = (model.BDL == null) ? -1 : model.BDL.Value;
            model.CDL = (model.CDL == null) ? -1 : model.CDL.Value;
            model.HDL = (model.HDL == null) ? -1 : model.HDL.Value;
            model.AGLYS = (model.AGLYS == null) ? -1 : model.AGLYS.Value;
            model.BGLYS = (model.BGLYS == null) ? -1 : model.BGLYS.Value;
            model.CGLYS = (model.CGLYS == null) ? -1 : model.CGLYS.Value;
            model.HGLYS = (model.HGLYS == null) ? -1 : model.HGLYS.Value;
            model.ASZGL = (model.ASZGL == null) ? -1 : model.ASZGL.Value;
            model.BSZGL = (model.BSZGL == null) ? -1 : model.BSZGL.Value;
            model.CSZGL = (model.CSZGL == null) ? -1 : model.CSZGL.Value;
            model.HSZGL = (model.HSZGL == null) ? -1 : model.HSZGL.Value;
            model.DJSCNJ = (model.DJSCNJ == null) ? -1 : model.DJSCNJ.Value;
            model.DJSCZS = (model.DJSCZS == null) ? -1 : model.DJSCZS.Value;
            model.DJXL = (model.DJXL == null) ? -1 : model.DJXL.Value;
            model.JSJSCNJ = (model.JSJSCNJ == null) ? -1 : model.JSJSCNJ.Value;
            model.QBZJ = (model.QBZJ == null) ? -1 : model.QBZJ.Value;
            model.LTWY = (model.LTWY == null) ? -1 : model.LTWY.Value;
            model.XDZH = (model.XDZH == null) ? -1 : model.XDZH.Value;
            model.YY = (model.YY == null) ? -1 : model.YY.Value;
            model.TY = (model.TY == null) ? -1 : model.TY.Value;
            model.YMGD = (model.YMGD == null) ? -1 : model.YMGD.Value;
            model.WD = (model.WD == null) ? -1 : model.WD.Value;
            model.YGNH = (model.YGNH == null) ? -1 : model.YGNH.Value;
            model.WGNH = (model.WGNH == null) ? -1 : model.WGNH.Value;

            return model;
        }
        /// <summary>
        /// 将采集静态数据经过转换算法转换后形成真正能入库的数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public BDDATESOURCEModel transBDDATESOURCEModel(BDDATESOURCEModel model)
        {
            model.WELLID = cjsjParser.getWELLIDParser(model.WELLID);
            model.JCRQ = cjsjParser.getJCRQParser(model.JCRQ);
            model.KSSJ = cjsjParser.getKSSJParser(model.KSSJ);
            model.JSSJ = cjsjParser.getJSSJParser(model.JSSJ);
            model.CJZSJ = cjsjParser.getCJZSJParser(model.CJZSJ);
            model.CJPL = cjsjParser.getCJPLParser(model.CJPL);
            model.CJZDS = 256;
            model.SCCDS = cjsjParser.getSCCDSParser(model.SCCDS);
            model.XCCDS = cjsjParser.getXCCDSParser(model.XCCDS);
            model.PULLID = cjsjParser.getPULLIDParser(model.PULLID);
            model.DJID = cjsjParser.getDJIDParser(model.DJID);
            model.PDGID = cjsjParser.getPDGIDParser(model.PDGID);
            model.GLDW = cjsjParser.getGLDWParser(model.GLDW);
            model.JCDW = cjsjParser.getJCDWParser(model.JCDW);
            model.CSR = cjsjParser.getCSRParser(model.CSR);
            model.CJSBXH = cjsjParser.getCJSBXHParser(model.CJSBXH);
            model.CJSBBB = cjsjParser.getCJSBBBParser(model.CJSBBB);
            model.SCRJBB = cjsjParser.getSCRJBBParser(model.SCRJBB);
            return model;
        }
    }
}